﻿
using System;
using Geotools.Geometries;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace AzukiMap.MapSource
{
    public abstract class RasterSource : MapSource
    {
        protected double _width;
        protected double _height;
        protected int _dpi;
        protected GTEnvelope _bbox;
        protected RasterFileSourceFormat _format;
        protected Uri _rasterSourceUri;
        protected Image _rasterImage;

        public GTEnvelope Bbox
        {
            get { return _bbox; }
            set { _bbox = value; }
        }

        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public RasterFileSourceFormat Format
        {
            get { return _format; }
            set { _format = value; }
        }

        public int Dpi
        {
            get { return _dpi; }
            set { _dpi = value; }
        }

        public virtual Uri ImageUri
        {
            get { return _rasterSourceUri; }
            set
            {
                _rasterSourceUri = value;
                SetImage();
            }
        }

        public Image RasterImage
        {
            get { return _rasterImage; }
        }

        public RasterSource(string name, MapSourceTypes type, int srid, CoordinationUnits coordUnit)
            : base(name, type, srid, coordUnit)
        {
            _rasterImage = new Image();
            _rasterImage.Loaded += new System.Windows.RoutedEventHandler(_rasterImage_Loaded);
            _dpi = 72;
        }

        void SetImage()
        {
            BitmapImage sourceImage = new BitmapImage();
            sourceImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(sourceImage_DownloadProgress);
            sourceImage.UriSource = _rasterSourceUri;
            _rasterImage.Source = sourceImage;
        }

        void sourceImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            if (e.Progress == 1)
            {
                _width = _rasterImage.ActualWidth;
                _height = _rasterImage.ActualHeight;
            }
        }

        void _rasterImage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _width = _rasterImage.ActualWidth;
            _height = _rasterImage.ActualHeight;
        }

        public override XElement ToXml()
        {
            return ToXml("RasterSource");
        }

        public override XElement ToXml(string elementName)
        {
            /*
            <xs:attributeGroup name="RasterSourceAttributes">
                <xs:attribute default="72" name="Dpi" type="xs:positiveInteger" />
                <xs:attribute name="Format" type="RasterFormat" />
                <xs:attribute name="ImageUri" type="Uri" />
                <xs:attribute name="Width" type="xs:nonNegativeInteger" />
                <xs:attribute name="Height" type="xs:nonNegativeInteger" />
            </xs:attributeGroup>
             */
            XElement rasterSourceElement = base.ToXml(elementName);
            rasterSourceElement.Add(
                _xmlCommon.CreateAttr("Width", _width),
                _xmlCommon.CreateAttr("Height", _height),
                _xmlCommon.CreateAttr("Dpi", _dpi),
                _xmlCommon.CreateAttr("Format", _format),
                _xmlCommon.CreateAttr("RasterSourceUri", _rasterSourceUri),
                _xmlCommon.EnvelopeToXml("BBox", _bbox)
                );
            return rasterSourceElement;
        }

        public override void Parse(XElement mapSource)
        {
            base.Parse(mapSource);
            XElement rasterSourceElem = mapSource.Element("RasterSource");
            _width = _xmlCommon.GetDoubleAttr(rasterSourceElem, "Width");
            _height = _xmlCommon.GetDoubleAttr(rasterSourceElem, "Height");
            _dpi = _xmlCommon.GetIntAttr(rasterSourceElem, "Dpi");
            _bbox = _xmlCommon.GetEnvelopeElement(rasterSourceElem, "BBox");
            _format = (RasterFileSourceFormat)
                _xmlCommon.GetEnumElement(rasterSourceElem, "Format", typeof(RasterFileSourceFormat));
            _rasterSourceUri = _xmlCommon.GetUriElement(rasterSourceElem, "RasterSourceUri");
        }

    }

    /// <summary>
    /// Silverlight supports PNG and JPEG only.
    /// </summary>
    public enum RasterFileSourceFormat
    {
        PNG,
        JPEG,
    }

}
